package com.renwei.yunlong.pager;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.CancelActivity;
import com.renwei.yunlong.activity.ChangeAttributesActivity;
import com.renwei.yunlong.activity.CloseActivity;
import com.renwei.yunlong.activity.ComplaintsActivity;
import com.renwei.yunlong.activity.DateSelecterActivity;
import com.renwei.yunlong.activity.EnterpriseInformationActivity;
import com.renwei.yunlong.activity.InformationActivity;
import com.renwei.yunlong.activity.NewDetailsActivity;
import com.renwei.yunlong.activity.RemarksActivity;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.base.BaseFragment;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.bean.WorkInformationBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.leefeng.promptlibrary.PromptButton;
import me.leefeng.promptlibrary.PromptButtonListener;
import me.leefeng.promptlibrary.PromptDialog;

import static okhttp3.Protocol.get;

/**
 * 工单  详情  页面
 * <p>
 * Created by Administrator on 2017/9/6.
 */

public class WorkDetailsPager extends BaseFragment implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.rl_unit)
    RelativeLayout rlUnit;//业主单位
    @Bind(R.id.rl_initiated)
    RelativeLayout rlInitiated;//发起人
    @Bind(R.id.rl_service_new)
    RelativeLayout rlServiceNew;//服务对象

    ACache mCache;

    @Bind(R.id.tv_number)
    TextView tvNumber;
    @Bind(R.id.tv_unit)
    TextView tvUnit;
    @Bind(R.id.tv_initiated)
    TextView tvInitiated;
    @Bind(R.id.tv_deal)
    TextView tvDeal;
    @Bind(R.id.tv_deal_person)
    TextView tvDealPerson;
    @Bind(R.id.tv_service_new)
    TextView tvServiceNew;
    @Bind(R.id.tv_project)
    TextView tvProject;
    @Bind(R.id.tv_service)
    TextView tvService;
    @Bind(R.id.tv_schedule)
    TextView tvSchedule;
    @Bind(R.id.tv_degree)
    TextView tvDegree;
    @Bind(R.id.tv_sla)
    TextView tvSla;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_initiated_time)
    TextView tvInitiatedTime;
    @Bind(R.id.tv_complete_time)
    TextView tvCompleteTime;
    @Bind(R.id.tv_description)
    TextView tvDescription;
    @Bind(R.id.tv_detailed)
    TextView tvDetailed;
    @Bind(R.id.btn_work)
    Button btnWork;

    Map<String, List<String>> map = new HashMap<String,List<String>>();
    List<String> list = null;
    PromptDialog promptDialog;
    ServiceLoginBean serviceLoginBean;

    @Override
    public View initView() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_work_details, null);
        ButterKnife.bind(this, view);
        mCache = ACache.get(getActivity());
        return view;
    }

    @Override
    public void initData() {
        rlUnit.setOnClickListener(this);
        rlInitiated.setOnClickListener(this);
        rlServiceNew.setOnClickListener(this);
        btnWork.setOnClickListener(this);

        String requestId = mCache.getAsString("requestId");

        if (!TextUtils.isEmpty(requestId)) {
            String jsonBean = "{requestId:\"" + requestId + "\"}";
            serviceUpcomingWorkHttp(jsonBean);
            Log.e("工单详情jsonBean", jsonBean);
        }

        serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");

        promptDialog = new PromptDialog(getActivity());
    }

    private void serviceUpcomingWorkHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.SERVICE_DETAIL_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    private void ownerUpcomingWorkHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(getActivity());
        hp.setUrl(Api.OWNER_DETAIL_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_unit:
                startActivity(new Intent(getActivity(), EnterpriseInformationActivity.class));
                break;
            case R.id.rl_initiated:
                startActivity(new Intent(getActivity(), InformationActivity.class));
                break;
            case R.id.rl_service_new:
                startActivity(new Intent(getActivity(), NewDetailsActivity.class));
                break;
            case R.id.btn_work:
                PromptButton cancle = new PromptButton("取消", null);
                //默认两个按钮为Alert对话框，大于三个按钮的为底部SHeet形式展现
                /**
                 *  new PromptButton("非现场", new PromptButtonListener() {
                @Override
                public void onClick(PromptButton button) {
                startActivity(new Intent(getActivity(), DateSelecterActivity.class));
                }
                }),
                 new PromptButton("转回", new PromptButtonListener() {
                @Override
                public void onClick(PromptButton button) {

                }
                }),
                 new PromptButton("启动", new PromptButtonListener() {
                @Override
                public void onClick(PromptButton button) {

                }
                }),
                 new PromptButton("挂起", new PromptButtonListener() {
                @Override
                public void onClick(PromptButton button) {

                }
                }),
                 new PromptButton("出发", new PromptButtonListener() {
                @Override
                public void onClick(PromptButton button) {

                }
                }),
                 new PromptButton("变更属性", new PromptButtonListener() {
                @Override
                public void onClick(PromptButton button) {
                startActivity(new Intent(getActivity(), ChangeAttributesActivity.class));
                }
                }),
                 new PromptButton("投诉", new PromptButtonListener() {
                @Override
                public void onClick(PromptButton button) {
                startActivity(new Intent(getActivity(), ComplaintsActivity.class));
                }
                }),
                 new PromptButton("备注", new PromptButtonListener() {
                @Override
                public void onClick(PromptButton button) {
                startActivity(new Intent(getActivity(), RemarksActivity.class));
                }
                })
                 */
                promptDialog.showAlertSheet("", true, cancle,
                        new PromptButton("关闭", new PromptButtonListener() {
                            @Override
                            public void onClick(PromptButton button) {
                                startActivity(new Intent(getActivity(), CloseActivity.class));
                            }
                        }), new PromptButton("取消", new PromptButtonListener() {
                            @Override
                            public void onClick(PromptButton button) {
                                startActivity(new Intent(getActivity(), CancelActivity.class));
                            }
                        }),
                        new PromptButton("指派给", new PromptButtonListener() {
                            @Override
                            public void onClick(PromptButton button) {
                                startActivity(new Intent(getActivity(), CancelActivity.class));
                            }
                        }),
                        new PromptButton("接受", new PromptButtonListener() {
                            @Override
                            public void onClick(PromptButton button) {
                                startActivity(new Intent(getActivity(), DateSelecterActivity.class));
                            }
                        }),
                        new PromptButton("去现场", new PromptButtonListener() {
                            @Override
                            public void onClick(PromptButton button) {
                                startActivity(new Intent(getActivity(), DateSelecterActivity.class));
                            }
                        }));

//                List<String> list = map.get("button");
//                if(null !=list && list.size() >0) {
//                    for (String string : list) {
//                        if ("关闭".equals(string)) {
//                            promptDialog.showAlertSheet("", true, cancle,
//                                    new PromptButton("关闭", new PromptButtonListener() {
//                                        @Override
//                                        public void onClick(PromptButton button) {
//                                            startActivity(new Intent(getActivity(), CloseActivity.class));
//                                        }
//                                    }));
//                        } else if ("取消".equals(string)) {
//                            promptDialog.showAlertSheet("", true, cancle,
//                                    new PromptButton("取消", new PromptButtonListener() {
//                                        @Override
//                                        public void onClick(PromptButton button) {
//                                            startActivity(new Intent(getActivity(), CancelActivity.class));
//                                        }
//                                    }));
//                        }
//                    }
//                }


                break;
            default:
                break;
        }
    }


    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                break;
            case 2:
                Log.e("工单详情数据", result);
                WorkInformationBean workInformationBean = new Gson().fromJson(result, WorkInformationBean.class);
                if (workInformationBean.getMessage().getCode() == 200) {

                    tvNumber.setText(workInformationBean.getRows().getWorkOrder());
                    tvUnit.setText(workInformationBean.getRows().getServerName());
                    tvInitiated.setText(workInformationBean.getRows().getSendUserName());
                    tvDeal.setText(workInformationBean.getRows().getServiceProviderName());
                    tvDealPerson.setText(workInformationBean.getRows().getResponseUserName());
                    tvServiceNew.setText(workInformationBean.getRows().getHardWareName());
                    //tvProject.setText(serviceBean.getRows().getP);//所属项目

                    tvService.setText(workInformationBean.getRows().getDirectoryName());

                    //tvSchedule.setText(serviceBean.getRows().);//执行程度
                    if ("1".equals(workInformationBean.getRows().getUrgency())) {
                        tvDegree.setText("高");
                    } else if ("2".equals(workInformationBean.getRows().getUrgency())) {
                        tvDegree.setText("中");
                    } else if ("3".equals(workInformationBean.getRows().getUrgency())) {
                        tvDegree.setText("低");
                    }
                    tvSla.setText(workInformationBean.getRows().getDays() + "天" + workInformationBean.getRows().getHours() + "小时");
                    tvType.setText(workInformationBean.getRows().getSendDate());
                    if ("0".equals(workInformationBean.getRows().getContractOut())) {
                        tvType.setText("否");
                    } else if ("1".equals(workInformationBean.getRows().getContractOut())) {
                        tvType.setText("是");
                    }
                    tvInitiatedTime.setText(workInformationBean.getRows().getSendDate());
                    tvCompleteTime.setText(workInformationBean.getRows().getCompleteDate());
                    tvDescription.setText(workInformationBean.getRows().getTitle());
                    tvDetailed.setText(workInformationBean.getRows().getRequestContent());
                    list = new ArrayList<String>();

                    //发起工单判断
                    if ("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId())
                            && !"10".equals(workInformationBean.getRows().getNowStat()) && !"5".equals(workInformationBean.getRows().getNowStat()) && !"11".equals(workInformationBean.getRows().getNowStat())){
                        //关闭
                        list.add("关闭");

                    }

                    if("0".equals(workInformationBean.getRows().getNowStat()) && workInformationBean.getRows().getSendUser().equals(serviceLoginBean.getRows().getEmployeeId())){
                        //取消
                        list.add("取消");
                    }

                    if("0".equals(workInformationBean.getRows().getNowStat()) || "18".equals(workInformationBean.getRows().getNowStat())){
                        if("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId())){
                            //指派给
                            list.add("指派给");

                        }
                    }else {
                        if("3".equals(workInformationBean.getRows().getNowStat()) && workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId())){
                            //接单  转回
                            list.add("接单");
                            list.add("转回");
                        }
                        if("1".equals(workInformationBean.getRows().getTreatment())){
                            if("4".equals(workInformationBean.getRows().getNowStat())){
                                if(workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId()) && (workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId()))|| ("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId()))){
                                    //挂起
                                    list.add("挂起");

                                }

                                if(workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId())){
                                    //出发 非现场  转回
                                    list.add("出发");
                                    list.add("非现场");
                                    list.add("转回");
                                }
                            }
                            if("16".equals(workInformationBean.getRows().getNowStat())){

                                if((workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId()))|| (("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId())))){
                                    //挂起

                                    list.add("挂起");
                                }

                                if(workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId())){
                                    //处理 到达  转回
                                    list.add("处理");
                                    list.add("到达");
                                    list.add("转回");
                                }
                            }
                            if("17".equals(workInformationBean.getRows().getNowStat())){

                                if((workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId()))|| (("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId())))){
                                    //挂起
                                    list.add("挂起");
                                }
                                if(workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId())){
                                    //处理  转回
                                    list.add("处理");
                                    list.add("转回");
                                    if (null != workInformationBean.getRows().getHardWareIds() && !TextUtils.isEmpty(workInformationBean.getRows().getHardWareIds())){
                                        //备件申请  变更属性
                                        list.add("备件申请");
                                        list.add("转回");
                                    }
                                }
                            }
                        }else{
                            if("4".equals(workInformationBean.getRows().getNowStat())){

                                if((workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId()))|| (("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId())))){
                                    //挂起
                                    list.add("挂起");
                                }

                                if(workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId())){
                                    //处理  去现场 转回
                                    list.add("处理");
                                    list.add("去现场");
                                    list.add("转回");

                                    if (null != workInformationBean.getRows().getHardWareIds() && !TextUtils.isEmpty(workInformationBean.getRows().getHardWareIds())){
                                        //备件申请  变更属性
                                        list.add("备件申请");
                                        list.add("变更属性");
                                    }
                                }
                            }
                        }
                        if("7".equals(workInformationBean.getRows().getNowStat())){
                            if((workInformationBean.getRows().getResponseUserId().equals(serviceLoginBean.getRows().getEmployeeId()))|| (("12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId())))){
                                //启动
                                list.add("启动");
                            }
                        }
                    }
                    map.put("button",list);
                }

                break;
            default:
                break;

        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
